package com.lois.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lois.mapper.DownloadMapper;
import com.lois.model.DownloadData;
import com.lois.model.LongestMatchColumnWidthData;
import com.lois.service.DownloadService;
import com.lois.utils.DownloadUtils;
import org.springframework.stereotype.Service;

import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author lois
 * @since 2020-03-02
 */
@Service
public class DownloadServiceImpl extends ServiceImpl<DownloadMapper, DownloadData> implements DownloadService {

    private List<DownloadData> data() {
        List<DownloadData> list = new ArrayList();
        for (int i = 0; i < 10; i++) {
            DownloadData data = new DownloadData();
            data.setString("字符串" + i);
            data.setDate(new Date());
            data.setDoubleData(0.56);
            list.add(data);
        }
        return list;
    }

    private List<List<String>> header() {
        List<List<String>> list = new ArrayList<List<String>>();
        List<String> head0 = new ArrayList<String>();
        head0.add("字符串" + System.currentTimeMillis());
        List<String> head1 = new ArrayList<String>();
        head1.add("数字" + System.currentTimeMillis());
        List<String> head2 = new ArrayList<String>();
        head2.add("日期" + System.currentTimeMillis());
        list.add(head0);
        list.add(head1);
        list.add(head2);
        return list;
    }

    private List<LongestMatchColumnWidthData> dataLong() {
        List<LongestMatchColumnWidthData> list = new ArrayList<LongestMatchColumnWidthData>();
        for (int i = 0; i < 10; i++) {
            LongestMatchColumnWidthData data = new LongestMatchColumnWidthData();
            data.setString("测试很长的字符串测试很长的字符串测试很长的字符串" + i);
            data.setDate(new Date());
            data.setDoubleData(1000000000000.0);
            list.add(data);
        }
        return list;
    }

    private List<List<Object>> dataList() {
        List<List<Object>> list = new ArrayList<List<Object>>();
        for (int i = 0; i < 10; i++) {
            List<Object> data = new ArrayList<Object>();
            data.add("字符串" + i);
            data.add(new Date());
            data.add(0.56);
            list.add(data);
        }
        return list;
    }

    @Override
    public void download(OutputStream outputStream) {
        // 简单写-写法1
        DownloadUtils.simpleWrite1(outputStream, DownloadData.class, data());

        // 简单写-写法2
//        DownloadUtils.simpleWrite2(outputStream, DownloadData.class, data());

        // 排除指定的列
//        DownloadUtils.excludeWrite(outputStream, DownloadData.class, data());

        // 导出指定的列
//        DownloadUtils.includeWrite(outputStream, DownloadData.class, data());

        // 分页写到同一个sheet
//        DownloadUtils.repeatedWriteToOneSheet(outputStream, DownloadData.class, data());

        // 写到不同的sheet,同一个对象 TODO
//        DownloadUtils.repeatedWriteToDiffSheetByOneHead(outputStream, DownloadData.class, data());

        // 写到不同的sheet 不同的对象 TODO
//        DownloadUtils.repeatedWriteToDiffSheetByDiffHead(outputStream, DownloadData.class, data());

        // 写图片
//        try {
//            DownloadUtils.imageWrite(outputStream, ImageData.class);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }

        // 自定义样式
//        DownloadUtils.styleWrite(outputStream, DownloadData.class, data());

        // 合并单元格
//        DownloadUtils.mergeWrite(outputStream, DownloadData.class,data());

        // 使用table去写入
//        DownloadUtils.tableWrite(outputStream, DownloadData.class, data());

        // 动态表头、不指定对象写
//        DownloadUtils.dynamicHeadWrite(outputStream, dataList(), header());

        // 自动列宽
//        DownloadUtils.longestMatchColumnWidthWrite(outputStream, LongestMatchColumnWidthData.class, dataLong());

        // 下拉，超链接等自定义拦截器
//        DownloadUtils.customHandlerWrite(outputStream, DownloadData.class, data());
    }

}
